#!/bin/sh
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/alias.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/init_xdg_cache.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"

export APP_INSTALL_NAME="comfyui"
export CURRENT_APP_PATH="${LMD_APPS_DIR}/ComfyUI"
echo 'App Dir: '${CURRENT_APP_PATH}

cd "$CURRENT_APP_PATH"
CONDA_PREFIX="./venv"
conda run -p $CONDA_PREFIX --no-capture-output pip install watchdog

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    URL=$HF_MIRROR
    # URL length
    URL_LENGTH=${#URL}
    # check the last character
    LAST_CHAR=${URL:URL_LENGTH-1:1}
    if [ "$LAST_CHAR" == "/" ]; then
        # If it's "/", then remove it.
        URL=${URL:0:URL_LENGTH-1}
    fi
    export HF_MIRROR="$URL"
    export HF_ENDPOINT="$URL"
    echo 'HF_ENDPOINT:'$URL
fi


START_PARAMS=""
if [ -f /etc/os-release ]; then
    echo "Linux"
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    echo "macOS"
    ARCH=$(uname -m)
    CONDA_INSTALL_SCRIPT_URL=$(uname -m)
    if [ "$ARCH" == "x86_64" ]; then
        START_PARAMS="--disable-cuda-malloc --use-split-cross-attention --cpu"
    elif [ "$ARCH" == "arm64" ]; then
        START_PARAMS=""
    fi
elif current_os_is_windows; then
    echo "Windows"
    if command -v nvidia-smi &>/dev/null; then
        echo "Found NVIDIA drivers and cuda"
        START_PARAMS=""
    else
        echo "The nvidia-smi was not found. Start with CPU support."
        START_PARAMS="--disable-cuda-malloc --use-split-cross-attention --cpu"
    fi
else
    echo "Unknown OS"
fi

# allowExternalAccess
APP_ENV_PATH="$LMD_BASE_INSTALL_SCRIPT_DIR/apps/comfyui/lmd_app_env"
LISTEN_PARAMS=""
if [ -f "$APP_ENV_PATH" ]; then
    source "$APP_ENV_PATH"

    if [ "$allowExternalAccess" -eq '1' ]; then
        LISTEN_PARAMS="--listen 0.0.0.0"
    fi
fi

if [ -n "$SERVER_PORT" ]; then
    PORT_PARAM="--port $SERVER_PORT"
fi

is_shallow_clone() {
    REPO_PATH=$1
    if [ -f "${REPO_PATH}/.git/shallow" ]; then
        return 0
    fi
    return 1
}

conver_shallow_git_repo() {
    TMP_REPO_PATH=$1
    TMP_REPO_BRANCH=$2
    if is_shallow_clone "$CURRENT_APP_PATH"; then
        echo "this is a shallow clone repo. convert to full clone repo."
        rm -rf "${CURRENT_APP_PATH}/.git"
        export GIT_HOST=${GITHUB_PROXY}"https://github.com"
        export GIT_FETCH_ALL_HISTORY="true"
        export GIT_REPO_URL="${GIT_HOST}/${TMP_REPO_PATH}"
        export GIT_REPO_BRANCH="$TMP_REPO_BRANCH"
        export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
        sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"
    else
        echo "this is a full clone repo. $CURRENT_APP_PATH"
    fi
}

conver_shallow_git_repo "comfyanonymous/ComfyUI" "master"
# conver_shallow_git_repo "ltdrdata/ComfyUI-Manager" "main"


export GIT_HOST=${GITHUB_PROXY}"https://github.com"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_flash_attn.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_nunchaku.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_modelscope.sh"

trap 'exit 0' INT

echo "XDG_CACHE_HOME $XDG_CACHE_HOME"
if [ -n "$GITHUB_PROXY" ]; then
    export GITHUB_ENDPOINT="${GITHUB_PROXY}https://github.com"

    # replace check time value to 7 days
    MANAGER_UTIL_PATH="./custom_nodes/ComfyUI-Manager/glob/manager_util.py"
    if [ -f $MANAGER_UTIL_PATH ]; then
        echo "replace check time value to 7 days"
        OLD_TIME_VALUE="    return time_difference <= 86400"
        NEW_TIME_VALUE="    return time_difference <= 604800"
        conda_run_cmd python "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/replace_str.py" "$MANAGER_UTIL_PATH" "$OLD_TIME_VALUE" "$NEW_TIME_VALUE"

        OLD_GET_TIME_CODE="    file_creation_time = os.path.getctime(file_path)"
        NEW_GET_TIME_CODE="    file_creation_time = os.path.getmtime(file_path)"
        conda_run_cmd python "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/replace_str.py" "$MANAGER_UTIL_PATH" "$OLD_GET_TIME_CODE" "$NEW_GET_TIME_CODE"
    fi

    cp -f "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/run.py" "$CURRENT_APP_PATH"
    cp -f "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/aiohttp_proxy.py" "$CURRENT_APP_PATH"
    cp -f "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/run-cm-cli.py" "$CURRENT_APP_PATH/custom_nodes/ComfyUI-Manager"
    cp -f "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/aiohttp_proxy.py" "$CURRENT_APP_PATH/custom_nodes/ComfyUI-Manager"

    echo "set GITHUB_ENDPOINT=${GITHUB_ENDPOINT} and use run.py"
    conda run -p $CONDA_PREFIX --no-capture-output python run.py $START_PARAMS $LISTEN_PARAMS $PORT_PARAM
else
    conda run -p $CONDA_PREFIX --no-capture-output python main.py $START_PARAMS $LISTEN_PARAMS $PORT_PARAM
fi


